﻿<style>
    #dictTable + .layui-table-view .layui-table-tool-temp {
        padding-right: 0;
    }

    #dictTable + .layui-table-view .layui-table-body tbody > tr td {
        cursor: pointer;
    }

    #dictTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click {
        background-color: #fff3e0;
    }

    #dictTable + .layui-table-view .layui-table-body tbody > tr.layui-table-click td:last-child > div:before {
        position: absolute;
        right: 6px;
        content: "\e602";
        font-size: 12px;
        font-style: normal;
        font-family: layui-icon !important;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }

</style>
<!-- 正文开始 -->
<div class="layui-fluid" style="padding-bottom: 0;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;">
                    <!-- 表格工具栏1 -->
                    <form class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline" style="max-width: 140px;">
                                <input id="dictName" class="layui-input" placeholder="DataSourceName"/>
                            </div>
                            <div class="layui-inline">
                                <button class="layui-btn icon-btn" lay-filter="dictTbSearch" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>Search
                                </button>
                            </div>
                        </div>
                    </form>
                    <!-- 数据表格1 -->
                    <table id="dictTable" lay-filter="dictTable"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body" style="padding: 10px;">
                    <!-- 表格工具栏2 -->
                    <form class="layui-form toolbar">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">ItemName:</label>
                                <div class="layui-input-inline">
                                    <input id="dictDataName" class="layui-input" placeholder="Plaese Input ItemName"/>
                                </div>
                            </div>
                            <!--<div class="layui-inline">
                                <label class="layui-form-label">ItemCode:</label>
                                <div class="layui-input-inline">
                                    <input id="dictDataCode" class="layui-input" placeholder="Plaese Input ItemCode"/>
                                </div>
                            </div>-->
                            <div class="layui-inline">&emsp;
                                <button class="layui-btn icon-btn" lay-filter="dictDataTbSearch" lay-submit>
                                    <i class="layui-icon">&#xe615;</i>Search
                                </button>
                            </div>
                        </div>
                    </form>
                    <!-- 数据表格2 -->
                    <table id="dictDataTable" lay-filter="dictDataTable"></table>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 表单弹窗1 -->
<script type="text/html" id="dictEditDialog">
    <form id="dictEditForm" lay-filter="dictEditForm" class="layui-form model-form">
        <input name="dictId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">Code:</label>
            <div class="layui-input-block">
                <input name="Code" placeholder="Please Input Code" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">Name:</label>
            <div class="layui-input-block">
                <input name="SourceName" placeholder="Please Input Name" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">Order:</label>
            <div class="layui-input-block">
                <input name="OrderNum" placeholder="Please Input OrderNum" class="layui-input" type="number"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">Remark:</label>
            <div class="layui-input-block">
                <textarea name="Remark" placeholder="Please Input Remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="dictEditSubmit" lay-submit>Save</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">Cancel</button>
        </div>
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="dictDataTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">Edit</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">Del</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="dictDataEditDialog">
    <form id="dictDataEditForm" lay-filter="dictDataEditForm" class="layui-form model-form" style="padding-left: 10px;">
        <input name="dictDataId" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required" style="padding-left: 0;width: 95px;">ItemName</label>
            <div class="layui-input-block">
                <input name="SourceName" placeholder="Please Input SourceName" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required" style="padding-left: 0;width: 95px;">ItemCode</label>
            <div class="layui-input-block">
                <input name="Code" placeholder="Please Input Code" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">SortNo</label>
            <div class="layui-input-block">
                <input name="OrderNum" placeholder="Please Input SortNo" class="layui-input" type="number"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">Remark</label>
            <div class="layui-input-block">
                <textarea name="Remark" placeholder="Please Input Remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="dictDataEditSubmit" lay-submit>Save</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">Cancel</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'setter'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var selObj;  // 左表选中数据
        var setter = layui.setter;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#dictTable',
            method: 'post',
            headers: { 'Authorization': setter.getToken() },
            contentType: 'application/json',
            url: layui.setter.appServer + 'Sys_DataSource/GetLeftList',
            height: 'full-200',
            page:false,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>add</button>&nbsp;',
                '<button lay-event="edit" class="layui-btn layui-btn-sm layui-btn-warm icon-btn"><i class="layui-icon">&#xe642;</i>edit</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>del</button>',
                '</p>'].join(''),
            defaultToolbar: [],
            cols: [[
                {type: 'numbers'},
                { field: 'SourceName', title: 'SourceName'}
            ]],
            done: function (res, curr, count) {
                $('#dictTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
            }
        });

        /* 表格搜索 */
        form.on('submit(dictTbSearch)', function (data) {
            insTb.reload({ where: { searchKey: $('#dictName').val()}});
            return false;
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(dictTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'edit') { // 修改
                showEditModel(selObj.data, selObj);
            } else if (obj.event === 'del') { // 删除
                doDel(selObj);
            }
        });

        /* 监听行单击事件 */
        table.on('row(dictTable)', function (obj) {
            selObj = obj;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            insTb2.reload({
                where: { searchKey: $('#dictDataName').val(),OtherId: obj.data.Id},
                url: layui.setter.appServer + 'Sys_DataSource/GetRightList',
            });
        });

        /* 显示表单弹窗 */
        function showEditModel(mData, obj) {
            admin.open({
                type: 1,
                title: (mData ? 'Edit' : 'Add') + 'DataSource',
                content: $('#dictEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('dictEditForm', mData);
                    // 表单提交事件
                    form.on('submit(dictEditSubmit)', function (data) {
                        
                        if (mData) {
                            for (x in data.field) {
                                mData[x] = data.field[x];
                            }
                        }
                        else {
                            mData = data.field;
                            mData.Id = 0;
                        }
                        
                        var loadIndex = layer.load(2);
                        admin.req(setter.appServer + "Sys_DataSource/SaveDataSource", JSON.stringify({ strEntity: JSON.stringify(mData) }), function (res) {

                            layer.close(loadIndex);
                            if (res.code === 0) {
                                layer.close(dIndex);
                                layer.msg('Success', { icon: 1 });
                                insTb.reload();
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'post');
                        return false;
                        //admin.req(mData ? 'ok.json' : 'ok.json', data.field, function (res) {
                        //    layer.close(loadIndex);
                        //    if (200 === res.code) {
                        //        layer.close(dIndex);
                        //        layer.msg(res.msg, {icon: 1});
                        //        if (obj) {
                        //            obj.update(data.field);
                        //        } else {
                        //            insTb.reload();
                        //        }
                        //    } else {
                        //        layer.msg(res.msg, {icon: 2});
                        //    }
                        //}, 'get');
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('Confirm Del？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                //admin.req('ok.json', {
                //    id: obj.data.dictId,
                //}, function (res) {
                //    layer.close(loadIndex);
                //    if (200 === res.code) {
                //        layer.msg(res.msg, {icon: 1});
                //        obj.del();
                //        $('#dictTable+.layui-table-view .layui-table-body tbody>tr:first').trigger('click');
                //    } else {
                //        layer.msg(res.msg, {icon: 2});
                //    }
                //}, 'get');
                var loadIndex = layer.load(2);
                admin.req(setter.appServer + "Sys_DataSource/DelSource", JSON.stringify({ Ids: [obj.data.Id] }), function (res) {

                    layer.close(loadIndex);
                    if (res.code === 0) {

                        layer.msg('Success', { icon: 1 });
                        insTb.reload({}, true);
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                    }
                }, 'post');
                return false;
            });
        }

        /* 渲染表格2 */
        var insTb2 = table.render({
            elem: '#dictDataTable',
            data: [],
            method: 'post',
            height: 'full-200',
            headers: { 'Authorization': setter.getToken() },
            contentType: 'application/json',
            
            page: false,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>add</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>del</button>&nbsp;',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                { field: 'SourceName', title: 'ItemName', sort: true},
                { field: 'Code', title: 'ItemCode', sort: true},
                { field: 'OrderNum', title: 'SortNo', sort: true, align: 'center', width: 90, minWidth: 90},
                { field: 'Remark', title: 'Remark', sort: true},
                {
                    field: 'CreateTime', title: 'CreateTime', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true
                },
                {title: 'OperateBar', toolbar: '#dictDataTbBar', align: 'center', width: 120, minWidth: 120}
            ]]
        });

        /* 表格2搜索 */
        form.on('submit(dictDataTbSearch)', function (data) {
            insTb2.reload({ where: { searchKey: $('#dictDataName').val(), OtherId: selObj.data.Id } });
            return false;
        });

        /* 表格2工具条点击事件 */
        table.on('tool(dictDataTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel2(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel2(obj);
            }
        });

        /* 表格2头工具栏点击事件 */
        table.on('toolbar(dictDataTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel2();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('dictDataTable');
                if (checkRows.data.length === 0) {
                    layer.msg('Please Select Del Dataes', {icon: 2});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.dictDataId;
                });
                doDel2({ids: ids});
            }
        });

        /* 显示表单弹窗2 */
        function showEditModel2(mData) {
            admin.open({
                type: 1,
                title: (mData ? 'edit' : 'add') + 'DataSourceItem',
                content: $('#dictDataEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('dictDataEditForm', mData);
                    // 表单提交事件
                    form.on('submit(dictDataEditSubmit)', function (data) {
                        if (mData) {
                            for (x in data.field) {
                                mData[x] = data.field[x];
                            }
                        }
                        else {
                            mData = data.field;
                            mData.Id = 0;
                        }
                        mData.SourceId = selObj.data.Id;
                        var loadIndex = layer.load(2);
                        admin.req(setter.appServer + "Sys_DataSource/SaveDataSourceItem", JSON.stringify({ strEntity: JSON.stringify(mData) }), function (res) {

                            layer.close(loadIndex);
                            if (res.code === 0) {
                                layer.close(dIndex);
                                layer.msg('Success', { icon: 1 });
                                insTb2.reload({},true);
                            } else {
                                layer.msg(res.msg, { icon: 2 });
                            }
                        }, 'post');
                        return false;
                    });
                }
            });
        }

        /* 删除2 */
        function doDel2(obj) {
            layer.confirm('Confirm Del？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                admin.req(setter.appServer + "Sys_DataSource/DelSourceItem", JSON.stringify({ Ids: Ids }), function (res) {

                    layer.close(loadIndex);
                    if (res.code === 0) {

                        layer.msg('Success', { icon: 1 });
                        insTb2.reload({}, true);
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                    }
                }, 'post');
                return false;
            });
        }

    });
</script>